package com.blockchain.securewallet.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.blockchain.securewallet.R;
import com.blockchain.securewallet.utils.Logger;
import com.blockchain.securewallet.utils.ToastUtils;

import java.util.List;
import java.util.logging.Handler;

import butterknife.BindView;
import butterknife.ButterKnife;


public class WordsAdapter extends RecyclerView.Adapter<WordsAdapter.WordsItem> {

    private List<String> words;

    private final LayoutInflater mLayoutInflater;
    private Context context;

    // 0 > 预览的TextView、
    // 1 > 点击输入的EditText
    // 2 > 键盘输入的EditText
    private int type = 0;

    private IWordChangeWatch watcher;
    private IWordClickListener iWordClickListener;
    public static boolean isTextChange = false;

    private ISeedClickListener iSeedClickListener;


    public WordsAdapter(Context context, List<String> words) {
        this.words = words;
        this.context = context;
        mLayoutInflater = LayoutInflater.from(context);
    }

    public void setType(int type) {
        this.type = type;
    }

    public List<String> getWords() {
        return words;
    }

    public void setWatcher(IWordChangeWatch watcher) {
        this.watcher = watcher;
    }

    public void setWordListener(IWordClickListener iWordClickListener) {
        this.iWordClickListener = iWordClickListener;
    }

    public void setiSeedClickListener(ISeedClickListener iSeedClickListener) {
        this.iSeedClickListener = iSeedClickListener;
    }

    public void setWords(List<String> words) {
        this.words = words;
    }

    @NonNull
    @Override
    public WordsItem onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (type == 0) {
            return new PreviewWordsItem(mLayoutInflater.inflate(R.layout.seed_item_preview, parent, false));
        } else if (type == 1) {
            return new EditWordsItem(mLayoutInflater.inflate(R.layout.seed_item_edit, parent, false));
        } else {
            return new EditWordsItem(mLayoutInflater.inflate(R.layout.seed_item_edit2, parent, false));
        }
    }


    @Override
    public void onBindViewHolder(@NonNull final WordsItem holder, final int position) {
        if (type == 0) {
            ((PreviewWordsItem) holder).tvWord.setText(words.get(position));
            ((PreviewWordsItem) holder).tvWord.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (iWordClickListener != null) {
                        isTextChange = true;

                        iWordClickListener.clickListener(position, words.get(position));

                        //将点击过的项目设置成灰色且不可点
                        ((PreviewWordsItem) holder).tvWord.setEnabled(false);
                    }
                }
            });
            
        } else if (type == 1) {
            ((EditWordsItem) holder).editWord.setText(words.get(position));
//            ((EditWordsItem)holder).editWord.setInputType(InputType.TYPE_NULL);

            ((EditWordsItem) holder).editWord.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //ToastUtils.showToast("你点击了" + position + ":" + words.get(position));
                    Logger.ds("你点击了" + position + ":" + words.get(position));

                    ((EditWordsItem) holder).editWord.setText("");

                    if (iSeedClickListener != null) {
                        iSeedClickListener.onSeedClick(position, words.get(position));
                    }
                }
            });
            
        } else {
            ((EditWordsItem) holder).editWord.setText(words.get(position));
        }

    }

    @Override
    public int getItemCount() {
        return words == null ? 0 : words.size();
    }


    abstract class WordsItem extends RecyclerView.ViewHolder {
        public WordsItem(View itemView) {
            super(itemView);
        }
    }

    class PreviewWordsItem extends WordsItem {

        @BindView(R.id.tv_word_item)
        TextView tvWord;

        public PreviewWordsItem(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class EditWordsItem extends WordsItem {

        @BindView(R.id.edit_word_item)
        EditText editWord;

        public EditWordsItem(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            editWord.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    if (watcher != null) {
                        watcher.dataChanged(getAdapterPosition(), s.toString());
                    }
                }
            });
        }

    }

    public interface IWordChangeWatch {
        void dataChanged(int pos, String text);
    }

    public interface IWordClickListener {
        void clickListener(int pos, String text);
    }

    public interface ISeedClickListener {
        void onSeedClick(int position, String value);
    }


}
